class Banner extends BaseSprite {

    //等待时间(单位:s)
    private static TIME_REST: number = 5;
    //动画时间(单位:ms)
    private static TIME_PLAYING: number = 300;

    private group: eui.Group;
    private groupMask: eui.Rect;
    private groupTips: eui.Group;
    private btn_left: eui.Image;
    private btn_right: eui.Image;

    private list: any[] = [];
    private index: number = 0;

    private turnTime: number = 0;

    private playing: boolean = false;

    constructor() {
        super();

        this.skinName = "BannerSkin";
    }

    public childrenCreated() {
        super.childrenCreated();

        this.width = 616;
        this.height = 310;

        this.group.mask = this.groupMask;

        this.checkShow();

        this.btn_left.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);
        this.btn_right.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);

        gameManager.timerManager.addEventListener(TimerManager.TIMER, this.timerHandler, this);
    }

    private timerHandler() {
        if (this.playing) return;
        if (!this.list || this.list.length <= 1) return;

        this.turnTime++;

        if (this.turnTime >= Banner.TIME_REST) {
            this.next();
        }
    }

    private clickHandler(e: egret.TouchEvent) {
        switch (e.currentTarget) {
            case this.btn_left:
                this.prev();
                break;
            case this.btn_right:
                this.next();
                break;
        }
    }

    update(list: any[]) {
        this.clear();

        if (!list || list.length == 0) return;

        this.list = list;
        this.index = 0;
        this.turnTime = 0;

        this.checkShow();
        this.resetTips();

        this.play();
    }

    private prev() {
        if (this.playing) return;

        if (this.index > 0) this.index--;
        else this.index = this.list.length - 1;

        this.play(false);
    }

    private next() {
        if (this.playing) return;

        if (this.index < this.list.length - 1) this.index++;
        else this.index = 0;

        this.play(true);
    }

    private play(toleft: boolean = true) {
        this.data = this.list[this.index];
        this.playing = true;
        this.turnTime = 0;

        let img: eui.Image;
        if (this.group.numElements == 0) {
            img = this.getImage(this.data.img);
            if (img) {
                this.group.addChild(img);

                this.playing = false;
            }
        }
        else if (this.group.numElements == 1) {
            let thiz = this;

            img = <eui.Image>this.group.getElementAt(0);
            if (img) {
                egret.Tween.get(img).to({x: toleft ? -this.width : this.width}, Banner.TIME_PLAYING).call(function () {
                    thiz.group.removeChildAt(0);
                });
            }

            img = this.getImage(this.data.img);
            if (img) {
                img.x = toleft ? this.width : -this.width;
                this.group.addChild(img);

                egret.Tween.get(img).to({x: 0}, Banner.TIME_PLAYING).call(function () {
                    thiz.playing = false;
                }, this);
            }
        }

        this.resetTips();
        if (this.index < this.groupTips.numElements) {
            img = <eui.Image>this.groupTips.getElementAt(this.index);
            if (img) {
                img.source = 'banner_selected';
            }
        }
    }

    clear() {
        this.list = [];
        this.index = 0;
        this.playing = false;
        this.turnTime = 0;
        this.group.removeChildren();
        this.groupTips.removeChildren();

        this.checkShow();
    }

    private resetTips() {
        if (this.groupTips.numElements == 0) {
            if (this.list && this.list.length) {
                let img: eui.Image;
                for (let i: number = 0; i < this.list.length; i++) {
                    img = new eui.Image();
                    img.source = 'banner_unselected';
                    this.groupTips.addChild(img);
                }
            }
        }
        else {
            let img: eui.Image;
            for (let i: number = 0; i < this.groupTips.numElements; i++) {
                img = <eui.Image>this.groupTips.getElementAt(i);
                if (img) {
                    img.source = 'banner_unselected';
                }
            }
        }
    }

    private checkShow() {
        this.groupTips.visible = this.btn_right.visible = this.btn_left.visible = this.list && this.list.length > 1;
    }

    private getImage(url: string): eui.Image {
        if (!url) {
            return null;
        }
        let img: eui.Image = new eui.Image();
        RES.getResByUrl(url, function (res) {
            if (res) {
                img.source = res;
            }
        }, this, RES.ResourceItem.TYPE_IMAGE);

        img.addEventListener(egret.TouchEvent.TOUCH_TAP, this.toSkip, this);

        return img;
    }

    private toSkip(e: egret.TouchEvent) {
        let img: eui.Image = <eui.Image>e.currentTarget;
        if (img && !this.playing && this.data && this.data.id) {
            let thiz = this;
            game.askPanel.showMsg(function (r) {
                if (r) {
                    location.href = GameConfig.welfare_href +
                        '?url=' + encodeURIComponent(Weixin.shareUrl()) +
                        '&gid=' + GameConfig.gameType +
                        '&gnm=' + GameConfig.mahjong_name +
                        '&oid=' + game.player.openid +
                        '&uid=' + game.player.uid +
                        '&pic=' + game.player.pic +
                        '&nick=' + game.player.nick +
                        '&aid=' + thiz.data.id +
                        '&v=' + Math.random();
                }
            }, "是否立即前往活动页面查看详情？");
        }
    }
}